class Vue {
    constructor(options) {
        this.$options = options
        this._data = options.data
        this.initData()
    }

    initData() {
        const data = this._data
        const keys = Object.keys(data)
        // 将data里所有key代理到vue实例上
        for(let key of keys) {
            Object.defineProperty(this, key, {
                enumerable: true,
                configurable: true,
                get: function proxyGetter() {
                    return data[key]
                },
                set: function proxySetter(val) {
                    data[key] = val
                }
            })
        }

        for(let key of keys) {
            let value = data[key]
            Object.defineProperty(this, key, {
                enumerable: true,
                configurable: true,
                get: function reactiveGetter(){
                    console.log(`data的${key}取值`)
                    return value
                },
                set: function reactiveSetter(val) {
                    if(val === value) {
                        return
                    }
                    console.log(`设置${key}为${val}`)
                    value = val
                }
            })
        }
    }
}
